﻿
Imports osi.root.constants

Public Module _non_integer_overflow
    Public Function uint8_int8(ByVal i As Byte) As SByte
        Return byte_sbyte(i)
    End Function

    Public Function int8_uint8(ByVal i As SByte) As Byte
        Return sbyte_byte(i)
    End Function

    Public Function byte_sbyte(ByVal i As Byte) As SByte
        Const int8_1 As SByte = 1
        Try
            If i > max_int8 Then
                Return CShort(i) - max_uint8 - int8_1
            Else
                Return i
            End If
        Catch
            k_assert(False)
            Return 0
        End Try
    End Function

    Public Function sbyte_byte(ByVal i As SByte) As Byte
        Const uint8_1 As Byte = 1
        Try
            If i < 0 Then
                Return CShort(i) + max_uint8 + uint8_1
            Else
                Return i
            End If
        Catch
            k_assert(False)
            Return 0
        End Try
    End Function

    Public Function uint16_int16(ByVal i As UInt16) As Int16
        Const int16_1 As Int16 = 1
        Try
            If i > max_int16 Then
                Return CInt(i) - max_uint16 - int16_1
            Else
                Return i
            End If
        Catch
            k_assert(False)
            Return 0
        End Try
    End Function

    Public Function int16_uint16(ByVal i As Int16) As UInt16
        Const uint16_1 As UInt16 = 1
        Try
            If i < 0 Then
                Return CInt(i) + max_uint16 + uint16_1
            Else
                Return i
            End If
        Catch
            k_assert(False)
            Return 0
        End Try
    End Function

    Public Function uint32_int32(ByVal i As UInt32) As Int32
        Const int32_1 As Int32 = 1
        Try
            If i > max_int32 Then
                Return CLng(i) - max_uint32 - int32_1
            Else
                Return i
            End If
        Catch
            k_assert(False)
            Return 0
        End Try
    End Function

    Public Function int32_uint32(ByVal i As Int32) As UInt32
        Const uint32_1 As UInt32 = 1
        Try
            If i < 0 Then
                Return CLng(i) + max_uint32 + uint32_1
            Else
                Return i
            End If
        Catch
            k_assert(False)
            Return 0
        End Try
    End Function

    Public Function uint64_int64(ByVal i As UInt64) As Int64
        Const int64_1 As Int64 = 1
        Try
            If i > max_int64 Then
                Return CDec(i) - max_uint64 - int64_1
            Else
                Return i
            End If
        Catch
            k_assert(False)
            Return 0
        End Try
    End Function

    Public Function int64_uint64(ByVal i As Int64) As UInt64
        Const uint64_1 As UInt64 = 1
        Try
            If i < 0 Then
                Return CDec(i) + max_uint64 + uint64_1
            Else
                Return i
            End If
        Catch
            k_assert(False)
            Return 0
        End Try
    End Function

    Public Function non_overflow_increment(ByRef i As Int32, Optional ByVal j As Int32 = 1) As Boolean
        Try
            i += j
        Catch
            Return False
        End Try

        Return True
    End Function

    Public Function non_overflow_increment(ByRef i As Int64, Optional ByVal j As Int64 = 1) As Boolean
        Try
            i += j
        Catch
            Return False
        End Try

        Return True
    End Function

    Public Function non_overflow_decrement(ByRef i As Int32, Optional ByVal j As Int32 = 1) As Boolean
        Try
            i -= j
        Catch
            Return False
        End Try

        Return True
    End Function

    Public Function non_overflow_decrement(ByRef i As Int64, Optional ByVal j As Int32 = 1) As Boolean
        Try
            i -= j
        Catch
            Return False
        End Try

        Return True
    End Function
End Module
